/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.service.impl;

import com.workcell.workdata.im.constant.EmoticonState;
import com.workcell.workdata.im.entity.Emoticon;
import com.workcell.workdata.im.entity.EmoticonItem;
import com.workcell.workdata.im.entity.EmoticonRecord;
import com.workcell.workdata.im.model.vo.EmoticonVO;
import com.workcell.workdata.im.repository.EmoticonItemRepository;
import com.workcell.workdata.im.repository.EmoticonRecordRepository;
import com.workcell.workdata.im.repository.EmoticonRepository;
import com.workcell.workdata.im.service.EmoticonService;
import com.workcell.workdata.im.util.PageX;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class EmoticonServiceImpl implements EmoticonService {

	@Resource
	private EmoticonRepository emoticonRepository;

	@Resource
	private EmoticonRecordRepository recordRepository;

	@Resource
	private EmoticonItemRepository itemRepository;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void add(long uid, long emoticonId) {

		Emoticon emoticon = emoticonRepository.findById(emoticonId).orElse(null);
		if (emoticon == null){
			return;
		}

		EmoticonRecord record = new EmoticonRecord();
		record.setCreateTime(new Date());
		record.setEmoticonId(emoticonId);
		record.setUid(uid);
		record.setState(EmoticonState.NORMAL.getValue());

		recordRepository.saveAndFlush(record);
	}

	@Override
	public List<EmoticonVO> findList(long uid) {

		List<EmoticonRecord> records = recordRepository.findList(uid);

		List<Long> idList = records.stream().map(EmoticonRecord::getEmoticonId).collect(Collectors.toList());

		Map<Long,Byte> stateMap = new HashMap<>();

		for (EmoticonRecord record : records){
			stateMap.put(record.getEmoticonId(), record.getState());
		}

		List<EmoticonVO> voList = PageX.map(EmoticonVO::of,emoticonRepository.findList(idList));

		for (EmoticonVO emoticon : voList){
			emoticon.setState(stateMap.get(emoticon.getId()));
			emoticon.setItemList(itemRepository.findList(emoticon.getId()));
		}

		return voList;
	}

	@Override
	public EmoticonVO findOne(long id) {
		return EmoticonVO.of(emoticonRepository.findById(id).orElse(null));
	}

	@Override
	public List<EmoticonItem> findItemList(long id) {
		return itemRepository.findList(id);
	}

	@Override
	public Page<EmoticonVO> queryPage(Pageable pageable) {
		Page<Emoticon> page = emoticonRepository.findAll(pageable);
		return PageX.map(EmoticonVO::of,page);
	}

	@Override
	public void disable(long uid, long emoticonId) {
		recordRepository.updateState(uid,emoticonId, EmoticonState.DISABLED.getValue());
	}

	@Override
	public void enable(long uid, long emoticonId) {
		recordRepository.updateState(uid,emoticonId, EmoticonState.NORMAL.getValue());
	}

}
